<?php

define('FETCH_TAGS', 9); // Fetch tags and relations

/**
 * Description of Util_UiContentProvider
 *
 * @author Young Suk Ahn
 */
class Util_ProfileHelper extends Util_Helperbase {
		
	protected $userService;

	private $messageService;
	private $profileService;

	public function __construct(&$request, &$requestContext)
	{
		parent::__construct($request, $requestContext);
		
		$this->userService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_USER.'/', ACCESS_TOKEN);
		$this->userService->init($this->_requestContext);


		$this->profileService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROFILE.'/', ACCESS_TOKEN);
		$this->profileService->init($requestContext);

		$this->messageService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_MESSAGE.'/', ACCESS_TOKEN);
		$this->messageService->init($requestContext);
	}

	public function getUserService() {
		return $this->userService;
	}
	public function getProfileService() {
		return $this->profileService;
	}
	public function getMessageService() {
		return $this->messageService;
	}
	
	public function getProfileList($criteria, $offset, $max)
	{
		return $this->profileService->getList($criteria, $offset, $max);
	}
	
	/**
	 * Loads profile given user sid retrieved from query
	 * @param type $fetchMode
	 * @return type 
	 */
	public function getProfile($userXid, $fetchMode)
	{
		$extraQueryParam = array('fetchMode' => $fetchMode);
		/*
		$xid = $this->request->query('userSid');
		if ($xid == NULL) {
			$xid = $this->_sessionInfo->userInfo['userSid'];
		}*/

		if ($userXid != NULL && $userXid > 0) {
			$profile = $this->profileService->getByXid($userXid, 'user', $extraQueryParam);
			return $profile;
		}
		return null;
	}
	
	public function setProfileIntoTemplate(&$template, $profile)
	{
		$template->set_global('profile', $profile);

		if ($profile->creatorSid == $this->_sessionInfo->userInfo['userSid'])
			$template->set_global('isSelfProfile', true);
		else 
			$template->set_global('isSelfProfile', false);
	}

	public function loadUnreadMessages(&$template)
	{
		$userSid = $this->_sessionInfo->userInfo['userSid'];
		$unreadCount = $this->messageService->getByXid($userSid, 'count/unread');
		$template->set_global('message_unreadCount', $unreadCount);
	}
	
	public function getWantToFromForm()
	{
		$wantTo_pursue = $this->request->post('wantTo_pursue');
		$wantTo_mentor = $this->request->post('wantTo_mentor');
		$wantTo_collaborate = $this->request->post('wantTo_collaborate');
		$wantTo_coordinate = $this->request->post('wantTo_coordinate');
		$wantTo = $wantTo_pursue | $wantTo_mentor | $wantTo_collaborate | $wantTo_coordinate;
		
		return $wantTo;
	}
	
	public function recordFromForm()
	{
		$record = array();
		$record['creatorSid'] = $this->_sessionInfo->userInfo['userSid'];
		$record['creatorDto'] = array();
		$record['creatorDto']['sid'] = $this->_sessionInfo->userInfo['userSid'];
		$record['creatorDto']['profileImage'] = $this->saveProfileImage($this->_sessionInfo->userInfo['userSid']);

		$record['sid'] = $this->request->post('sid');
		$record['domainId'] = $this->request->post('domainId');
		$record['uuid'] = $this->request->post('uuid');
		$record['firstName'] = $this->request->post('firstName');
		$record['lastName'] = $this->request->post('lastName');
		$record['lcName'] = $this->request->post('lcName');
		$record['currentLocation'] = $this->request->post('currentLocation');
		$record['hometown'] = $this->request->post('hometown');
		$record['gender'] = $this->request->post('gender');
		$record['birthDate'] = '2012-11-10T09:08:07';//$this->request->post('planStartDate');
		$record['highlight'] = $this->request->post('highlight');
		$record['goals'] = $this->request->post('goals');
		$record['philosophy'] = $this->request->post('philosophy');
		$record['wantTo'] = $this->getWantToFromForm();

		$record['languageTagNamesCsv'] = $this->request->post('languageTagNamesCsv');
		$record['skillTagNamesCsv'] = $this->request->post('skillTagNamesCsv');
		$record['interestTagNamesCsv'] = $this->request->post('interestTagNamesCsv');

		$record['status'] = $this->request->post('status');
		$record['timeCommitment'] = $this->request->post('timeCommitment');
		
		return $record;
	}
	
	
	private function saveProfileImage($creatorSid)
	{
		if (!isset($_FILES["profileImage"]))
			return null;
		$profileImage = array();
		$profileImageFile = $_FILES["profileImage"];
		if ($profileImageFile["error"] > 0) {
			//echo "Error: " . $profileImageFile["error"] . "<br />";
			$profileImage = null;
		} else {
			$profileImage['fileName'] = $profileImageFile["name"];
			$profileImage['contentType'] = $profileImageFile["type"];
			$profileImage['fileSize'] = (int)$profileImageFile["size"];
			//echo "Stored in: " . $_FILES["file"]["tmp_name"];
			
			$repoPath = MEDIA_FILE_DIR.'/'.$creatorSid.'/media/';
			if (!file_exists($repoPath)) {
				mkdir($repoPath, 0700, true);
			}
			$fullPath = $repoPath.$profileImage['fileName'];
			$copied = copy($profileImageFile['tmp_name'], $fullPath);
			$profileImage['repositoryUri'] = $repoPath;
			
			$imageInfo = getimagesize($fullPath);
			if ($imageInfo) {
				$profileImage['width'] =  $imageInfo[0]; 
				$profileImage['height'] = $imageInfo[1];
				$profileImage['contentType'] = $imageInfo['mime']  ;
			}
			
		}
		
		return $profileImage;
	}
}
